package com.example.qsmx;

import com.example.qsmx.Util.MyDBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class QSMXValue {
    private String id;
    private String tittle;
    private String content;
    private String createTime;
    private String valueUserAccount;
    private String cover;

    public QSMXValue(String id, String tittle, String content, String createTime, String valueUserAccount, String cover) {
        this.id = id;
        this.tittle = tittle;
        this.content = content;
        this.createTime = createTime;
        this.valueUserAccount = valueUserAccount;
        this.cover = cover;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTittle() {
        return tittle;
    }

    public void setTittle(String tittle) {
        this.tittle = tittle;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getValueUserAccount() {
        return valueUserAccount;
    }

    public void setValueUserAccount(String valueUserAccount) {
        this.valueUserAccount = valueUserAccount;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public static List<QSMXValue> getQSMXValue() throws SQLException {
        // 创建对应的集合，准备存储内容
        List<QSMXValue> QSMXValueList = new ArrayList<>();

        // 连接数据库
        Connection connection = null;
        connection = MyDBUtil.getConnection();

        // 创建 sql 语句获取数据库中所有的内容
        String valueSql = "SELECT * FROM qsmxvalue";
        PreparedStatement preparedStatement;
        try {
            preparedStatement = connection.prepareStatement(valueSql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //获取数据集
        ResultSet valueResultSet;
        try {
            valueResultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //处理数据集，并存入对应集合中
        while (valueResultSet.next()) {
            String value_id = valueResultSet.getString("value-id");
            String value_tittle = valueResultSet.getString("value-tittle");
            String value_content = valueResultSet.getString("value-content");
            String value_create_time = valueResultSet.getString("value-create-time");
            String value_userAccount = valueResultSet.getString("value-userAccount");
            String value_cover = valueResultSet.getString("value-cover");
            if (value_cover == null){
                value_cover = "img/default-cover.png";
            }

            QSMXValue v = new QSMXValue(value_id, value_tittle, value_content, value_create_time, value_userAccount, value_cover);
            QSMXValueList.add(v);
        }
        return QSMXValueList;
    }

    public static List<QSMXValue> getMyQSMXValue(String u) throws SQLException {
        // 创建对应的集合，准备存储内容
        List<QSMXValue> QSMXValueList = new ArrayList<>();

        // 连接数据库
        Connection connection = null;
        connection = MyDBUtil.getConnection();

        // 创建 sql 语句获取数据库中所有的内容
        String valueSql = "SELECT * FROM qsmxvalue WHERE `value-userAccount` = ?";
        PreparedStatement preparedStatement;
        try {
            preparedStatement = connection.prepareStatement(valueSql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // 上传数据
        preparedStatement.setString(1, u);

        //获取数据集
        ResultSet valueResultSet;
        try {
            valueResultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //处理数据集，并存入对应集合中
        while (valueResultSet.next()) {
            String value_id = valueResultSet.getString("value-id");
            String value_tittle = valueResultSet.getString("value-tittle");
            String value_content = valueResultSet.getString("value-content");
            String value_create_time = valueResultSet.getString("value-create-time");
            String value_userAccount = valueResultSet.getString("value-userAccount");
            String value_cover = valueResultSet.getString("value-cover");
            if (value_cover == null){
                value_cover = "img/default-cover.png";
            }

            QSMXValue v = new QSMXValue(value_id, value_tittle, value_content, value_create_time, value_userAccount, value_cover);
            QSMXValueList.add(v);
        }
        return QSMXValueList;
    }

    public static List<QSMXValue> getViewQSMXValue(String valueTitle) throws SQLException {
        // 创建对应的集合，准备存储内容
        List<QSMXValue> QSMXValueList = new ArrayList<>();

        // 连接数据库
        Connection connection = null;
        connection = MyDBUtil.getConnection();

        // 创建 sql 语句获取数据库中所有的内容
        String valueSql = "SELECT * FROM qsmxvalue WHERE `value-tittle` LIKE ?";
        PreparedStatement preparedStatement;
        try {
            preparedStatement = connection.prepareStatement(valueSql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // 加入查找元素
        preparedStatement.setString(1, "%" + valueTitle + "%");

        //获取数据集
        ResultSet valueResultSet;
        try {
            valueResultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //处理数据集，并存入对应集合中
        while (valueResultSet.next()) {
            String value_id = valueResultSet.getString("value-id");
            String value_tittle = valueResultSet.getString("value-tittle");
            String value_content = valueResultSet.getString("value-content");
            String value_create_time = valueResultSet.getString("value-create-time");
            String value_userAccount = valueResultSet.getString("value-userAccount");
            String value_cover = valueResultSet.getString("value-cover");
            if (value_cover == null){
                value_cover = "img/default-cover.png";
            }

            QSMXValue v = new QSMXValue(value_id, value_tittle, value_content, value_create_time, value_userAccount, value_cover);
            QSMXValueList.add(v);
        }
        return QSMXValueList;
    }

    public static List<QSMXValue> getMyViewQSMXValue(String u, String valueTitle) throws SQLException {
        // 创建对应的集合，准备存储内容
        List<QSMXValue> QSMXValueList = new ArrayList<>();

        // 连接数据库
        Connection connection = null;
        connection = MyDBUtil.getConnection();

        // 创建 sql 语句获取数据库中所有的内容
        String valueSql = "SELECT * FROM qsmxvalue WHERE `value-userAccount` = ? AND `value-tittle` LIKE ?";
        PreparedStatement preparedStatement;
        try {
            preparedStatement = connection.prepareStatement(valueSql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // 上传数据
        preparedStatement.setString(1, u);

        // 加入查找元素
        preparedStatement.setString(2, "%" + valueTitle + "%");

        //获取数据集
        ResultSet valueResultSet;
        try {
            valueResultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //处理数据集，并存入对应集合中
        while (valueResultSet.next()) {
            String value_id = valueResultSet.getString("value-id");
            String value_tittle = valueResultSet.getString("value-tittle");
            String value_content = valueResultSet.getString("value-content");
            String value_create_time = valueResultSet.getString("value-create-time");
            String value_userAccount = valueResultSet.getString("value-userAccount");
            String value_cover = valueResultSet.getString("value-cover");
            if (value_cover == null){
                value_cover = "img/default-cover.png";
            }

            QSMXValue v = new QSMXValue(value_id, value_tittle, value_content, value_create_time, value_userAccount, value_cover);
            QSMXValueList.add(v);
        }
        return QSMXValueList;
    }
}
